package com.ns.scs.vsb.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ns.core.annotations.ReqLog;
import com.ns.core.bo.IdsBO;
import com.ns.core.utils.sg.ResultOk;
import com.ns.scs.vsb.bo.ViewBoardBO;
import com.ns.scs.vsb.bo.ViewBoardPageBO;
import com.ns.scs.vsb.dto.ViewBoardPageDto;
import com.ns.scs.vsb.entry.ViewBoard;
import com.ns.scs.vsb.service.IViewBoardService;
import com.ns.scs.vsb.vo.ViewBoardVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * 看板数据业务接口
 *
 * @author lw
 * @since 2024-11-08
 */
@RestController
@RequestMapping("/vsb")
@RequiredArgsConstructor
@CrossOrigin
public class ViewBoardController {

    private final IViewBoardService viewBoardService;

    /**
     * 增
     *
     * @param bo 参数
     */
    @PostMapping("add")
    @ReqLog(module = "可视化模块", name = "增")
    @Transactional(rollbackFor = Exception.class)
    public ResultOk add(@RequestBody ViewBoardBO bo) {
        ViewBoard viewBoard = new ViewBoard();
        BeanUtils.copyProperties(bo, viewBoard);
        viewBoard.setSort(viewBoardService.count() + 1);
        return ResultOk.getByBoolean(viewBoardService.saveOrUpdate(viewBoard));
    }

    /**
     * 逻辑删
     *
     * @param id 参数
     */
    @GetMapping("delete")
    @ReqLog(module = "可视化模块", name = "逻辑删")
    public ResultOk delete(Long id) {
        return ResultOk.getByBoolean(viewBoardService.removeById(id));
    }

    /**
     * 删 - 根据id列表
     *
     * @param bo 参数
     */
    @PostMapping("deleteByIds")
    @ReqLog(module = "可视化模块", name = "批量删")
    public ResultOk deleteByIds(@RequestBody IdsBO bo) {
        return ResultOk.getByBoolean(viewBoardService.removeBatchByIds(bo.getIds()));
    }

    /**
     * 改
     *
     * @param bo 参数
     */
    @PostMapping("update")
    @ReqLog(module = "可视化模块", name = "改")
    public ResultOk update(@RequestBody ViewBoardBO bo) {
        ViewBoard viewBoard = new ViewBoard();
        BeanUtils.copyProperties(bo, viewBoard);
        return ResultOk.getByBoolean(viewBoardService.saveOrUpdate(viewBoard));
    }

    /**
     * 查
     *
     * @param id 参数
     */
    @GetMapping("getById")
    @ReqLog(module = "可视化模块", name = "查")
    public ResultOk getById(long id) {
        ViewBoard viewBoard = viewBoardService.getById(id);
        ViewBoardVO vo = new ViewBoardVO();
        BeanUtils.copyProperties(viewBoard, vo);
        return ResultOk.getSuccessData(vo);
    }

    /**
     * 查 - 集合
     *
     * @param bo 参数
     */
    @PostMapping("getList")
    @ReqLog(module = "租户模块", name = "集合")
    public ResultOk getList(@RequestBody ViewBoardPageBO bo) {
        ViewBoardPageDto dto = new ViewBoardPageDto();
        BeanUtils.copyProperties(bo, dto);
        Page<ViewBoardVO> page = viewBoardService.getList(dto);
        return ResultOk.getPageData(page.getTotal(), page.getRecords());
    }

    /**
     * 可视化视图数据
     *
     * @param key 参数
     */
    @GetMapping("board")
    @ReqLog(module = "可视化模块", name = "看板数据")
    public ResultOk getViewBoard(String key) {
        return ResultOk.getSuccessData(viewBoardService.getViewBoard(key));
    }
}
